//
//  ZYXCourseViewController.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/2.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXCourseViewController.h"
#import "ZYXCourseCell.h"

#import "ZYXChooseCourseVC.h"
#import "ZYXClassificationModel.h"
@interface ZYXCourseViewController ()
@property (strong,nonatomic) NSMutableArray *dataArr;
@end

@implementation ZYXCourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"课程"];
}
#pragma mark MainUI
-(void)setupViews{
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
    fl.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(HEIGHT(10), 0, SCREEN_WIDTH-HEIGHT(20), SCREEN_HEIGHT-NavigationBar_Height-TabBar_Height) collectionViewLayout:fl];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = YES;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZYXCourseCell" bundle:nil] forCellWithReuseIdentifier:@"course"];
    [self.view addSubview:self.collectionView];
}
#pragma mark 加载数据
- (void)loadData {
    
    [NetWork GETWithUrlString:kInterfaceAllUrl(PATH_XuankeIndex) parmas:@{} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            NSArray *modelArr = [ZYXClassificationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            self.dataArr = [NSMutableArray arrayWithArray:modelArr];
            [self.collectionView reloadData];
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        
    } failureBlock:^(NSError *error) {
        
    }];
}
#pragma mark collectionDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  
    return self.dataArr.count;
  
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        ZYXCourseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"course" forIndexPath:indexPath];
        cell.model = self.dataArr[indexPath.row];
        return cell;
   
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

        CGFloat TwoCellW = (SCREEN_WIDTH -HEIGHT(40))/2.0f;
        CGFloat TwoCellH = HEIGHT(162);
        return CGSizeMake(TwoCellW, TwoCellH);
 
}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//
//        return UIEdgeInsetsMake(0, HEIGHT(10), 0, HEIGHT(20));
//
//
//}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击事件");
    ZYXClassificationModel *model = self.dataArr[indexPath.row];
    ZYXChooseCourseVC *vc = [[ZYXChooseCourseVC alloc]init];
    vc.title = model.title;
    vc.mid = model.uid;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 懒加载
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
